package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"softwareEngineering/models"
	"strconv"
)

type AdminController struct {
	beego.Controller
}

func (c *AdminController) Get() {
	user := c.Ctx.Input.Session("user").(models.User)
	c.splitArtickeContent(user)
	headImg, _ := QueryImageById(user.ImgId)
	beego.Info("headImg", headImg)
	c.Data["headImg"] = headImg
	c.Data["videos"] = QueryVideoByUserId(strconv.Itoa(user.Id))
	c.Data["images"] = QueryAllImage(user)
	c.Data["articles"] = QueryAllArticle(user)
	c.Data["user"] = user
	c.TplName = "admin.html"
}
func (c *AdminController) DeleteArticle() {
	user := c.Ctx.Input.Session("user").(models.User)
	articleId := c.GetString("article")
	artId, _ := strconv.Atoi(articleId)
	flag := DelArticle(artId)
	if flag == false {
		c.Data["deleteSuccess"] = "deleteFailure"
		c.TplName = "tip.html"
	}
	c.splitArtickeContent(user)
	//c.Data["articles"] = QueryAllArticle(user)
	c.Data["deleteSuccess"] = "deleteSuccess"
	c.TplName = "tip.html"

}

func (c *AdminController) DeleteVideo() {
	videoId := c.GetString("video")
	vidId, _ := strconv.Atoi(videoId)
	flag := DelVideo(vidId)
	if flag == true {
		c.Data["deleteSuccess"] = "deleteFailure"
		c.TplName = "tip.html"
	}
	c.Data["deleteSuccess"] = "deleteSuccess"
	c.TplName = "tip.html"
}

func (c *AdminController) PlayVideo() {
	videoPath := c.GetString("videoPath")
	c.Data["videoPath"] = videoPath
	c.TplName = "simple-video.html"
}

// 文章内容截取
func (c *AdminController) splitArtickeContent(user models.User) {

	articles := QueryAllArticle(user)
	as := articles
	for i := 0; i < len(as); i++ {
		as[i].Content = as[i].Content[0:54]
	}
	c.Data["as"] = as
}

func (c *AdminController) DeployArticle() {
	user := c.Ctx.Input.Session("user").(models.User)
	articleId := c.GetString("articleId")
	beego.Info(articleId)
	number, _ := strconv.Atoi(articleId)
	article := QueryArticleById(number)
	c.Data["images"] = QueryAllImage(user)
	c.Data["article"] = article
	c.TplName = "articlePre.html"

}

//图片点击操作
func (c *AdminController) ShowImage() {
	user := c.Ctx.Input.Session("user").(models.User)
	path := c.GetString("path")
	c.Data["path"] = path
	c.Data["images"] = QueryAllImage(user)
	c.TplName = "imageShow.html"
}

//切换图片查看模式
func (c *AdminController) ImageShow() {
	user := c.Ctx.Input.Session("user").(models.User)
	path := c.GetString("path")
	c.Data["path"] = path
	c.Data["images"] = QueryAllImage(user)

	c.TplName = "imageShow.html"

}

func (c *AdminController) ImageShow2() {
	user := c.Ctx.Input.Session("user").(models.User)
	path := c.GetString("path")
	c.Data["path"] = path
	c.Data["images"] = QueryAllImage(user)
	c.TplName = "imageShow2.html"
}
func (c *AdminController) DeleteImages() {
	imageId := c.GetString("imageId")
	beego.Info(imageId)
	id, _ := strconv.Atoi(imageId)
	b := DeleImageById(id)
	if b == true {
		c.Data["deleteImageSuccess"] = "deleteImageSuccess"

	} else {
		c.Data["deleteImageSuccess"] = "deleteImageFailure"
	}
	c.TplName = "tip.html"
}

func (c *AdminController) CheckArticle() {
	articleId := c.GetString("article")

	artId, _ := strconv.Atoi(articleId)

	articleContent, mdContent := ArticleContent(artId)
	c.Data["articleContent"] = articleContent
	c.Data["mdContent"] = mdContent
	c.TplName = "articleShow.html"
}

// Book跳转处理
func (c *AdminController) Book() {
	user := c.Ctx.Input.Session("user").(models.User)
	c.Data["images"] = QueryAllImage(user)
	c.TplName = "articlePre.html"

}

func (c *AdminController) BookSubmit() {
	book := models.Book{}
	page := c.GetString("page")
	p, _ := strconv.Atoi(page)
	book.Id = p
	homepage := c.GetString("homePage")
	title := c.GetString("title")
	content := c.GetString("content")
	imgId := c.GetString("imgId")
	deploy := c.GetString("deploy")
	beego.Info(book)
	o := orm.NewOrm()
	err := o.Read(&book)
	if err != nil {
		book.HomePage = homepage
		book.Title = title
		book.Content = content
		book.ImgId = imgId
		book.Deploy = deploy
		_, err := o.Insert(&book)
		if err != nil {
			beego.Info("插入失败")

		} else {
			beego.Info("插入成功")
			c.Data["BookInsertSucc"] = "BookInsertSucc"
			c.TplName = "tip.html"
		}
	} else {
		//是否进行更新操作
		book.HomePage = homepage
		book.Title = title
		book.Content = content
		book.ImgId = imgId
		book.Deploy = deploy
		_, err := o.Update(&book)
		if err != nil {
			beego.Info("更新失败")

		} else {
			c.Data["BookUpdateSucc"] = "BookUpdateSucc"
			c.TplName = "tip.html"
		}
	}
	//user := c.Ctx.Input.Session("user").(models.User)
	//c.Data["images"] = QueryAllImage(user)
	//c.TplName = "articlePre.html"
}

type UserInformation struct {
	Name  string
	Pwd   string
	Role  string
	ImgId string
}

//用户个人信息修改
func (c *AdminController) UpdateInformation() {
	user := c.Ctx.Input.Session("user").(models.User)
	name := c.GetString("name")
	pwd := c.GetString("pwd")
	role := c.GetString("role")
	imgId := c.GetString("imgId")
	flag := UpdateInfor(user.Id, name, pwd, role, imgId)
	beego.Info(name, pwd, role, imgId, flag)
	c.Data["UpdateInfSuc"] = "UpdateInfSuc"
	c.TplName = "tip.html"

}

//修改Book页面数据
func (c *AdminController) ShowPageInforById() {
	user := c.Ctx.Input.Session("user").(models.User)

	id := c.GetString("page")
	page := QueryBooKPageContentById(id)
	if len(page) == 0 {
		c.Data["pageNoExit"] = "pageNoExit"
		c.TplName = "tip.html"
	} else {
		c.Data["page"] = page[0]
		c.Data["images"] = QueryAllImage(user)
		c.TplName = "articlePre.html"
	}
}
